Skip to content

Improve performance by replacing broadcasting with built-in function#279

Open
TzuYaoHuang wants to merge 6 commits intoWaterLily-jl:masterfrom
TzuYaoHuang:broadcastToBuiltInFun
Open

Improve performance by replacing broadcasting with built-in function#279
TzuYaoHuang wants to merge 6 commits intoWaterLily-jl:masterfrom
TzuYaoHuang:broadcastToBuiltInFun

Conversation

@TzuYaoHuang
Copy link
Copy Markdown
Member

  • A .= B can be copyto!(A,B).
  • A .= a can be fill!(A,a)
  • A.*=a can be rmul!(A,a)
  • A./=a can be rmul!(A,inv(a))

@TzuYaoHuang
Copy link
Copy Markdown
Member Author

TODO: benchmark test

@TzuYaoHuang
Copy link
Copy Markdown
Member Author

This PR imropve the speed between 1-7 %

Benchmark environment: tgv sim_step! (max_steps=400)
▶ log2p = 6
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    20589017 │   0.00 │    10.36 │            98.78 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    20520217 │   0.00 │     9.60 │            91.51 │     1.08 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │     5742991 │   1.14 │     1.17 │            11.20 │     8.82 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │     5634601 │   1.20 │     1.15 │            10.95 │     9.02 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘
▶ log2p = 7
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    27767223 │   0.75 │    44.42 │            52.96 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    27698423 │   0.79 │    44.09 │            52.56 │     1.01 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │     8052372 │   0.35 │     4.54 │             5.41 │     9.79 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │     7928372 │   0.30 │     4.34 │             5.18 │    10.23 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘
▶ log2p = 8
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    22614409 │   0.10 │   234.70 │            34.97 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    22545609 │   0.12 │   223.60 │            33.32 │     1.05 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │     6627673 │   0.07 │    22.24 │             3.31 │    10.55 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │     6518980 │   0.07 │    20.74 │             3.09 │    11.31 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘

Benchmark environment: cylinder sim_step! (max_steps=400)
▶ log2p = 5
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    49289315 │   0.43 │    88.22 │            62.32 │     1.00 │
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    49340973 │   0.43 │    87.94 │            62.12 │     1.00 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │    12928028 │   9.73 │     8.67 │             6.13 │    10.14 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │    12795064 │   9.54 │     7.77 │             5.49 │    11.32 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘
▶ log2p = 6
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    57328213 │   0.16 │   508.07 │            44.86 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    57259413 │   0.16 │   491.39 │            43.39 │     1.03 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │    14792974 │   1.30 │    56.29 │             4.97 │     9.03 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │    14609425 │   1.56 │    51.59 │             4.56 │     9.85 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘
▶ log2p = 7
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    48802921 │   0.01 │  3043.64 │            33.60 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    48734121 │   0.01 │  2963.06 │            32.71 │     1.03 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │    12886489 │   0.22 │   372.04 │             4.11 │     8.18 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │    12718992 │   0.26 │   329.76 │             3.64 │     9.23 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘

Benchmark environment: jelly sim_step! (max_steps=400)
▶ log2p = 6
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    40943020 │   1.28 │    37.53 │            89.47 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    40874220 │   1.30 │    37.18 │            88.65 │     1.01 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │    11495064 │   0.52 │     3.51 │             8.38 │    10.68 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │    11333211 │   0.53 │     3.32 │             7.92 │    11.30 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘
▶ log2p = 7
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    45088849 │   0.25 │   201.15 │            59.95 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    45020049 │   0.26 │   197.45 │            58.85 │     1.02 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │    12534774 │   0.15 │    18.43 │             5.49 │    10.91 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │    12370391 │   0.11 │    16.93 │             5.05 │    11.88 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘
▶ log2p = 8
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    59468573 │   0.05 │  1387.09 │            51.67 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    59399773 │   0.05 │  1341.87 │            49.99 │     1.03 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │    16092945 │   0.04 │   150.38 │             5.60 │     9.22 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │    15943248 │   0.05 │   138.26 │             5.15 │    10.03 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘

Benchmark environment: oscillatingSphere sim_step! (max_steps=1000)
▶ log2p = 6
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    93040526 │   2.51 │    35.73 │           136.31 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    92800918 │   2.67 │    34.41 │           131.28 │     1.04 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │    22688538 │   1.67 │     4.62 │            17.63 │     7.73 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │    22441462 │   1.26 │     4.57 │            17.42 │     7.82 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘
▶ log2p = 7
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │   104104091 │   0.88 │   115.72 │            55.18 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │   103932091 │   0.91 │   115.12 │            54.90 │     1.01 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │    26425908 │   0.68 │    13.43 │             6.41 │     8.61 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │    26156899 │   0.61 │    12.75 │             6.08 │     9.07 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘
▶ log2p = 8
┌────────────┬───────────┬────────┬───────────┬─────────────┬────────┬──────────┬──────────────────┬──────────┐
│    Backend │ WaterLily │  Julia │ Precision │ Allocations │ GC [%] │ Time [s] │ Cost [ns/DOF/dt] │ Speed-up │
├────────────┼───────────┼────────┼───────────┼─────────────┼────────┼──────────┼──────────────────┼──────────┤
│     CPUx16 │   Master  │ 1.12.5 │   Float32 │    92477265 │   0.16 │   617.58 │            36.81 │     1.00 │
│     CPUx16 │   This PR │ 1.12.5 │   Float32 │    92375660 │   0.17 │   598.47 │            35.67 │     1.03 │
│ GPU-NVIDIA │   Master  │ 1.12.5 │   Float32 │    23540740 │   0.16 │    65.27 │             3.89 │     9.46 │
│ GPU-NVIDIA │   This PR │ 1.12.5 │   Float32 │    23312159 │   0.14 │    61.68 │             3.68 │    10.01 │
└────────────┴───────────┴────────┴───────────┴─────────────┴────────┴──────────┴──────────────────┴──────────┘

@TzuYaoHuang TzuYaoHuang marked this pull request as ready for review April 5, 2026 10:59
@weymouth
Copy link
Copy Markdown
Member

weymouth commented Apr 5, 2026

This is ok, but I wouldn't generally bother tuning this stuff. 5% isn't nothing, but what made you want to fix it? Ideally, the compiler with do this automatically.

@TzuYaoHuang
Copy link
Copy Markdown
Member Author

Nothing particular. Just porting what I found useful in IA.jl to WL.jl as a bit of leisure work. Though, switching to fill! is partly for style consistency, since some parts use it while others still use .=.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants